using System.Linq;
using NodeCanvas.DialogueTrees;
using UnityEngine;

public class DialogueManger : MonoBehaviour
{
    private static DialogueManger _instance;

    public static DialogueManger instance
    {
        get
        {
            if (_instance == null) _instance = new DialogueManger();

            return _instance;
        }
    }

    /// <summary>
    ///     根据剧情表名为动态生成对话数
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="tableName"></param>
    public void CreatDialogueTree(DialogueTree dt, string tableName)
    {
        //移除上一次的对话  每次都动态重新生成
        foreach (var VARIABLE in dt.allNodes) dt.RemoveNode(VARIABLE);


        DataManager.Init(true);
        var query = new DatabaseQuery().SelectFrom(
            new[] {"name", "plot", "branch"},
            tableName
        );
        var rest = DataManager.GetAttrs(query);
        //获取到对应名字
        var name = rest.Values.ElementAt(0);
        //获取到剧情
        var plot = rest.Values.ElementAt(1);
        //获取到对应分支
        var branch = rest.Values.ElementAt(2);
        //当前行数
        int index = 0;
        //是否跳过这次查找进入下次查找
        bool endFind=false;
        StatementNode curNode = new StatementNode();
        MultipleChoiceNode curChoiceNode = new MultipleChoiceNode();
        foreach (var col in plot)
        {
            if (endFind)
            {
                endFind = false;
                continue ;
            }
            switch (branch[index])
            {
                //正常顺序分支
                case 0:
                    var seqNode = dt.AddNode<StatementNode>(curNode.position+new Vector2(0, 100));
                    seqNode.statement.text = col;
                    //修改了actorName属性  自己进去改为public
                    seqNode.actorName = name[index];
                    if (index != 0) dt.ConnectNodes(curNode, seqNode);
                    curNode = seqNode;
                    index++;
                    break;
                //遇到选择分支
                case 1:
                    //生成一个选择节点
                    var choseNode = dt.AddNode<MultipleChoiceNode>(curNode.position+new Vector2(0, 100));
                    //用curChoiceNode记下当前的选择节点
                    curChoiceNode = choseNode;
                    dt.ConnectNodes(curNode, choseNode);
                    index++;
                    break;
                //选择条件 一般在选择分支下面 动态生成完完整分支才会进入分支(选择 顺序)
                case 2:
                    //修改了availableChoices属性 自己进去改为public
                    curChoiceNode.availableChoices.Add(
                        new MultipleChoiceNode.Choice(new Statement(plot[index])));
                    var nextNode = dt.AddNode<StatementNode>(curChoiceNode.position+new Vector2(40*index, 200 ));
                    nextNode.statement.text = plot[index + 1];
                    dt.ConnectNodes(curChoiceNode, nextNode);
                    curNode = nextNode;
                    //下一次的查找不执行
                    endFind = true;
                    index += 2;
                    break;
                //对话结束 最后一个节点 根据任务编号发送信息给Mgr,Mgr做出相应处理
                case 4:
                    
                    break;
            }
        }
        //可行的方法二
        /*for (int index = 0; index < plot.Count; index++)
        {
            switch (branch[index])
            {
                //正常顺序分支
                case 0:
                    var seqNode = dt.AddNode<StatementNode>(new Vector2(0, 100 * index));
                    seqNode.statement.text = plot[index];
                    //修改了actorName属性  自己进去改为public
                    seqNode.actorName = name[index];
                    if (index != 0) dt.ConnectNodes(curNode, seqNode);
                    curNode = seqNode;
                   // index++;
                    break;
                //遇到选择分支
                case 1:
                    //生成一个选择节点
                    var choseNode = dt.AddNode<MultipleChoiceNode>(new Vector2(0, 100 * index));
                    //用curChoiceNode记下当前的选择节点
                    curChoiceNode = choseNode;
                    dt.ConnectNodes(curNode, choseNode);
                   // index++;
                    break;
                //选择条件 一般在选择分支下面 动态生成完完整分支才会进入分支(选择 顺序)
                case 2:
                    // Debug.Log("123134156");
                    //var newChose = new MultipleChoiceNode.Choice();
                    //newChose.statement.text = plot[index];
                    //修改了availableChoices属性 自己进去改为public
                    curChoiceNode.availableChoices.Add(
                        new MultipleChoiceNode.Choice(new Statement(plot[index])));
                    var seqNode123 = dt.AddNode<StatementNode>(new Vector2(0, 100 * index));
                    seqNode123.statement.text = plot[index + 1];
                    dt.ConnectNodes(curChoiceNode, seqNode123);
                    curNode = seqNode123;
                    index ++;
                    break;
            }
        }*/

        DataManager.End();
    }

    private void Start()
    {
    }
}